package com.llkj.qingnianguoxue.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.EveryDayAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.EveryDayBean;
import com.llkj.qingnianguoxue.bean.HotRankingBean;
import com.llkj.qingnianguoxue.book.ReadBookActivity;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.ClickViewCallback;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.RxBus;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;
import com.llkj.qingnianguoxue.view.ReadActivity;

import java.util.ArrayList;
import java.util.List;

import rx.functions.Action1;

import static com.llkj.qingnianguoxue.home.VideoDetailActivity.REFRESH_STATE;
import static com.llkj.qingnianguoxue.view.ReadActivity.REQUEST_READ_BOOK_CODE;

/**
 * 每日进一步
 */
public class EveryDayActivity extends BaseActivity implements ClickViewCallback, BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    private static final int REQUEST_DETAIL_CODE = 6976;
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvActions;
    private EveryDayAdapter todayAdapter;
    private List<EveryDayBean.DayBean> todayData;
    private int totalSize, mPage,mCurrentPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_every_day);
        setTitle("范儿足迹", true, TITLE_TYPE_IMG, R.drawable.icon_back, true, TITLE_TYPE_IMG, R.drawable.icon_clear_foot);

        initView();

//        loadWaitDialog();
        onRefresh();
    }

    private void initView() {
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);

        lvActions = (RecyclerView) findViewById(R.id.recyclerView);
        int padding = DisplayUtil.dip2px(this, 10);
        lvActions.setPadding(0, 0, 0, padding);
        lvActions.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        todayData = new ArrayList<>();
        todayAdapter = new EveryDayAdapter(R.layout.item_every_day1, todayData, this);
        lvActions.setAdapter(todayAdapter);

        refreshLayout.setOnRefreshListener(this);
        todayAdapter.setOnLoadMoreListener(this);
        RxBus.getInstance().toObservable(String.class).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                if (s.equals(REFRESH_STATE)) {
                    onRefresh();
                }
            }
        });

        lvActions.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                if(view.getId()== R.id.tvDelItem){
                    mCurrentPosition = i;
                    EveryDayBean.DayBean dayBean = todayData.get(mCurrentPosition);
                    loadWaitDialog();
                    HttpMethod.delShopping(EveryDayActivity.this,EveryDayActivity.this,dayBean.getId(),"1");
                }
            }
        });
    }

    @Override
    protected void rightDoWhat() {
        super.rightDoWhat();
        dialogConfirm("提示","确定要删除所有足迹吗？","取消","确定").show();
    }

    @Override
    protected void dialogRightBtn() {
        super.dialogRightBtn();
        loadWaitDialog();
        HttpMethod.pubDelAll(this,this);
    }

    @Override
    public void clickViewCallback(int position, int type, View view) {
        EveryDayBean.DayBean dayBean = todayData.get(position);
        type = dayBean.getType();// 类型 1 视频2 图书 3 天
        if (type == 1) {//视频
            Bundle bundle = new Bundle();
            bundle.putString("id", dayBean.getContent_id());
            startActivity(VideoDetailActivity.class, bundle);
        } else if (type == 2) {//图书
            if (1 == dayBean.getIs_buy()) {
//                Bundle bundle = new Bundle();
//                bundle.putString("id", dayBean.getId());
//                bundle.putString("pdfUrl", dayBean.getFile());
//                bundle.putString("bookName", dayBean.getTitle());
//                String percent = StringUtils.isEmpty(dayBean.getPlays_percent()) ? "0%" : dayBean.getPlays_percent();
//                bundle.putString("percent", percent);
//                String page = StringUtils.isEmpty(dayBean.getRead_page()) ? "0" : dayBean.getRead_page();
//                bundle.putString("readPage", page);
//                startActivity(ReadBookActivity.class, bundle);
                Intent intent = new Intent(this, ReadActivity.class);
                intent.putExtra("bookurl",dayBean.getDetails_file());
                intent.putExtra("bookid",dayBean.getId());
                intent.putExtra("bookname",dayBean.getTitle());
                intent.putExtra("bookDesc",dayBean.getShare());
                intent.putExtra("bookCoverUrl",dayBean.getImg());
                intent.putExtra("bookbegin",0);
                intent.putExtra("payMoney",dayBean.getPrice());
                intent.putExtra("isBuy",dayBean.getIs_buy());
                startActivityForResult(intent,REQUEST_READ_BOOK_CODE);
            } else {
                Bundle bundle = new Bundle();
                bundle.putString("id", dayBean.getContent_id());
                startActivityForResult(BookDetailActivity.class, bundle, REQUEST_DETAIL_CODE);
            }
        }
    }

    @Override
    public void onLoadMore() {
        mPage++;
        getData();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getData();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, todayAdapter, null, lvActions, totalSize);
    }

    private void getData() {
        HttpMethod.artistNobreak(this, this,String.valueOf(mPage));
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_ARTIST_NOBREAK) {
            EveryDayBean dayBean = GsonUtil.GsonToBean(data, EveryDayBean.class);
            if (dayBean != null) {
                totalSize = Integer.parseInt(dayBean.getNum());
                if (mPage == 1) {
                    todayData.clear();
                    LoadDataHelper.refreshComplete(todayAdapter, todayData, refreshLayout, lvActions);
                } else {
                    todayAdapter.setEnableLoadMore(true);
                    todayAdapter.loadMoreComplete();
                }
                todayData.addAll(dayBean.getFootprint());
                todayAdapter.notifyDataSetChanged();
            }
        }else if (method == HttpMethod.HTTP_USER_DEL_SHOPPING) {//8.13删除购物车商品
            ToastUtil.makeLongText(this, "删除成功");
            todayData.remove(mCurrentPosition);
            todayAdapter.notifyDataSetChanged();
        }else if (method == HttpMethod.HTTP_PUB_DELETE_ALL) {//清除足迹
            ToastUtil.makeLongText(this, "删除成功");
            todayData.clear();
            todayAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            onRefresh();
        }
    }
}
